import 'package:flutter/material.dart';
import 'package:merchants/models/attr_value.dart';

class AttrValueEditWidget extends StatelessWidget {
  final String attrNameId;
  final String? editingAttrValueId;
  final List<AttrValue> tags;
  final TextEditingController newTagController;
  final VoidCallback onAddTag;
  final VoidCallback onCancelTag;
  final VoidCallback Function(String) onSaveTag;

  const AttrValueEditWidget({
    super.key,
    required this.attrNameId,
    required this.editingAttrValueId,
    required this.tags,
    required this.newTagController,
    required this.onAddTag,
    required this.onCancelTag,
    required this.onSaveTag,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (editingAttrValueId == attrNameId)
            Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: newTagController,
                    decoration: const InputDecoration(
                      labelText: '输入新标签',
                      border: OutlineInputBorder(),
                    ),
                  ),
                ),
                const SizedBox(width: 8),
                TextButton(
                  onPressed: onSaveTag(attrNameId),
                  child: const Text('保存'),
                ),
                TextButton(onPressed: onCancelTag, child: const Text('取消')),
              ],
            )
          else
            Row(
              children: [
                ElevatedButton(onPressed: onAddTag, child: const Text('添加值')),
              ],
            ),
          if (tags.isNotEmpty) ...[
            const SizedBox(height: 8),
            Wrap(
              spacing: 8,
              children:
                  tags.map((tag) => Chip(label: Text(tag.value))).toList(),
            ),
          ],
        ],
      ),
    );
  }
}
